﻿using System;
using System.Threading;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.Common;
using Echo.Core.Common.WebObjects;
using Echo.Core.TestAbstraction.WebEnvironment;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Jannette Hamilton
////Date created: June 11, 2014
////Modified by: 
////Date modified: 
////Reason: https://ultidev/browse/ULTI-129493, https://ultidev/browse/ULTI-134167
////Run Time: ~6mins (3 BROWSERS)
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules
{
    [TestFixture]
    [Author("Jannette Hamilton", "Jannette_Hamilton@ultimatesoftware.com")]
    [Category("BusinessRules: Organization Levels")]
    [Header("Config")]
    [Config(".NET")]
    //[Config("IEOnly")]
    //[Config("ChromeOnly")]
    //[Config("FFOnly")]

    public class InactivateOrgLevels : MbUnitAutomatedTest
    {
        private UltiPro UltiPro;
        private UltiProDatabaseContext Database;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            Database.Company.AddRollBack("OrgLevel", "OrgCode = 'ACCTG'");

            // CHECK ORG STATUS BEFORE CHANGE
            Database.Company.Verify("select OrgCode, OrgDesc, OrgLvl, OrgStatus from OrgLevel where OrgCode = 'ACCTG'", "LocationBusinessRules/InactivateOrgLevels_ActiveACCTG.xml");
            
            // GIVEN THAT A SYSTEM ADMIN LOGS IN
            UltiPro.Pages.Login.LoginAs("JanH", "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("CALEN", "C0014");
            
            // WHEN AN ORGANIZATION LEVEL STATUS IS CHANGED TO INACTIVE
            UltiPro.Macros.Navigate.ToBusinessRule("Department");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Find(code => code.CodeFilter, FilterType.Is, "ACCTG");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailNonCodeTableGrid.Row(column => column.CodeLink, "ACCTG").Click(x => x.CodeLink);
            UltiPro.Pages.OrgLevelAddChg.Click(radioBtn => radioBtn.InactiveRadioButton);
            UltiPro.Pages.OrgLevelAddChg.ButtonBar.Click(btn => btn.Save);
            System.Threading.Thread.Sleep(1500); // NEED TO WAIT FOR TABLE TO UPDATE

            // THEN THE ORGLEVEL TABLE IS UPDATED AND THE CHANGES (NOTED IN EACH TEST) ARE SEEN IN THE GUI
            Database.Company.Verify("select OrgCode, OrgDesc, OrgLvl, OrgStatus from OrgLevel where OrgCode =  'ACCTG'", "LocationBusinessRules/InactivateOrgLevels_InactiveACCTG.xml");
            // TestSupport.Utilities.ResetIIS(Environment.UltiPro.WebServer); // NOT USING B/C NEED TO RE-LOGIN
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
           UltiPro.Browser.Quit();
           Database.Company.RollBackDatabase();
        }

        [SetUp]
        public void SetUp()
        {
        }

        [TearDown]
        public void TearDown()
        {
        }

        [Test(Order = 1)]
        public void VerifyAcctgDeptNotDisplayedInRoeAdmin()
        {
            UltiPro.Menu.Reporting.Click(link => link.ROEAdministration);
            UltiPro.Pages.ROEAdministration.ThingsICanDo.Click(link => link.CreateOriginalAndAmendedROEs);
            UltiPro.Pages.CreateROEs.SpecifyingEmployeesGrid.Find(findBy => findBy.OrgLevel3, FilterType.Is, "");
            UltiPro.Pages.CreateROEs.SpecifyingEmployeesGrid.DoesNotHaveOptions(option => option.TypeFilter, "innerHTML", "ACCTG");
        }

        [Test(Order = 2)]
        public void VerifyAcctgDeptNotDisplayedInManagePriorQrtrAdjustments()
        {
            UltiPro.Menu.PayrollProcessing.Click(link => link.PayrollGateway);
            UltiPro.Pages.PayrollGateway.ThingsICanDo.Click(link => link.ManagePriorQuarterAdjustmentsLink);
            UltiPro.Pages.WPREmployeeAdjustmentSummary.EmployeeAdjustmentSummaryGrid.Find(x => x.Department, FilterType.Is, "");
            UltiPro.Pages.WPREmployeeAdjustmentSummary.EmployeeAdjustmentSummaryGrid.DoesNotHaveOptions(option => option.TypeFilter, "innerHTML", "ACCTG");
        }

        [Test(Order = 3)]
        public void VerifyDepartmentIsDisplayedInCanadaYearend()
        {
            UltiPro.Menu.Reporting.Click(link => link.YearEndForms);
            UltiPro.Pages.YEForms.Set(dDown => dDown.CountryDropdown, "CAN");
            UltiPro.Pages.YEFormsCAN.Set(dDown => dDown.ActionsDropdown,  "PRINT FORMS", SelectOption.Value);
            UltiPro.Pages.YEForms.YeFormsCANGrid.Row(0).Click(column => column.Checkbox);
            UltiPro.Pages.YEForms.Click(button => button.OkButton);
            UltiPro.Pages.YEFormsPrintOptions.SortOrderMover.HasOption(option => option.Source, "Department", SelectOption.Text);
        }

        [Test(Order = 4)]
        public void VerifyInactivatedDepartmentIsDisplayedInStandardReports()
        {
            UltiPro.Macros.Navigate.ToAvailableReport("Tax Liability Grand Total");
            UltiPro.Pages.SRRequestReportCriteria.Set(dDown => dDown.SelectionDropdown, "Department", SelectOption.Text);
            UltiPro.Pages.SRRequestReportCriteria.CodeTableOptionMover("Accounting");
        }
    }
}